//
//  MyViewController.m
//  DianDangDai
//
//  Created by bhf on 2017/12/14.
//  Copyright © 2017年 bhf. All rights reserved.
//

#import "MyViewController.h"
#import "MyCell.h"
#import "UIView+WZLBadge.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "TabBarController.h"
#import "TuPianViewController.h"
@interface  MyViewController() <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSArray *imgArray;
@property (nonatomic, strong) NSArray *titleArray;

@end

@implementation MyViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
   
    NSString *login = [[NSUserDefaults standardUserDefaults] stringForKey:@"login"];
    if ([login isEqualToString:@"Yes"]) {
        _bgView.hidden = YES;
        _loginView.hidden = YES;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViews];
    _imgArray = @[@"w2",@"w1",@"w3",@"w4",@"w5",@"w6",@"w7",@"w8",@"w9"];
    _titleArray = @[@"投资账单",@"借款账单",@"我的奖励",@"我的礼包",@"积分商城",@"债权转让",@"我要评估",@"交易记录",@"安全设置"];
    [self setupViewsTouch];
    // [self showLoginView];
    // Do any additional setup after loading the view.
}

- (void)setupViews {
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1/*延迟执行时间*/ * NSEC_PER_SEC));
    
    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
        self.headImage.layer.cornerRadius = self.headImage.width/2;
        self.headImage.clipsToBounds = YES;
    });
   
    self.login.layer.cornerRadius = 40/2;
    self.regist.layer.cornerRadius = 40/2;
    _chongzhi.layer.cornerRadius = 30/2;
    _tixian.layer.cornerRadius= 30/2;
    _login.layer.borderColor = [[UIColor colorWithHex:@"#007aff"] CGColor];
    _login.layer.borderWidth = 1;
    self.navigationItem.backBarButtonItem = BarButtonItem;
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"userInformation"];
    NSLog(@"%@",dict);
    NSInteger badge = [dict[@"unMessage"] integerValue];
    [self.message showBadgeWithStyle:WBadgeStyleNumber value:badge animationType:WBadgeAnimTypeNone];
    self.account.text = dict[@"username"];
    NSString *imgURL = [NSString stringWithFormat:@"%@",dict[@"headImg"]];
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:[UIImage imageNamed:@"userImg"]];
    float aa = [dict[@"accountAmount"] floatValue];
    float ab = [dict[@"availableBalance"] floatValue];
    float fz = [dict[@"freeze"] floatValue];
    
    NSString *accountAmount = [self notRounding:aa afterPoint:2];
    NSString *availableBalance = [self notRounding:ab afterPoint:2];
    NSString *freeze = [self notRounding:fz afterPoint:2];
    
    self.accountAmount.text = accountAmount;
    self.availableBalance.text = availableBalance;
    self.freeze.text = freeze;
}

//登录界面

- (void)showLoginView {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    LoginViewController *login = [sb instantiateViewControllerWithIdentifier:@"login"];
    login.view.frame = CGRectMake(login.view.x, login.view.y, login.view.width, login.view.height-40);
    login.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentViewController:login animated:NO completion:nil];
    
  
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _imgArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *const ID = @"my_cell";
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.img.image = [UIImage imageNamed:_imgArray[indexPath.item]];
    cell.lab.text = _titleArray[indexPath.item];
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake((self.collectionView.frame.size.width-2)/3, (self.collectionView.frame.size.height-22)/3);
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identfier;
    if (indexPath.row == 0) {
        identfier = @"myInvestment";
    } else if (indexPath.row == 1) {
        identfier = @"myBrrowMoney";
    } else if (indexPath.row == 2) {
        identfier = @"award";
    } else if (indexPath.row == 3) {
        identfier = @"Coupon";
    } else if (indexPath.row == 4) {
        [self.view makeToast:@"投资积分可兑换精美礼品，正在开发中，敬请期待" duration:2 position:@"bottom"];
    } else if (indexPath.row == 5) {
        identfier = @"zhaiquan";
    } else if (indexPath.row == 6) {
        identfier = @"evc";
    } else if (indexPath.row == 7) {
        identfier = @"jiaoyi";
    } else if (indexPath.row == 8) {
        identfier = @"setup";
    }
    NSLog(@"%@",identfier);
    if (identfier.length>0) {

         [self showViewControllerWithIdentifier:identfier];
    }
   
}

- (IBAction)message:(UIButton *)sender {
//    TuPianViewController *vc = [[TuPianViewController alloc] init];
//    [self showViewController:vc sender:self];
    [self showViewControllerWithIdentifier:@"message"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)login:(UIButton *)sender {
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    UIViewController *vc = [board instantiateViewControllerWithIdentifier:@"alc"];
    vc.hidesBottomBarWhenPushed = YES;
    [self presentViewController:vc animated:YES completion:nil];
}
- (IBAction)regist:(UIButton *)sender {
    
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    UIViewController *vc = [board instantiateViewControllerWithIdentifier:@"regist"];
    vc.hidesBottomBarWhenPushed = YES;
    [self presentViewController:vc animated:YES completion:nil];
    
}
- (IBAction)tixian:(id)sender {

     [self showViewControllerWithIdentifier:@"tixian"];
}

//添加手势
- (void)setupViewsTouch {
    UITapGestureRecognizer *tapGesturRecognizer= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    _headImage.userInteractionEnabled = YES;
    [_headImage addGestureRecognizer:tapGesturRecognizer];

}

-(void)tapAction:(id)tap {

    NSLog(@"1111");
    [self showViewControllerWithIdentifier:@"MyIformation"];
}

/*-(void)hiddenAction:(id)tap {
    self.bgView.hidden = YES;
    self.loginView.hidden = YES;
}*/

- (IBAction)chongzhi:(id)sender {
 
    [self showViewControllerWithIdentifier:@"recharge"];
}

- (IBAction)showQRCode:(UIButton *)sender {
    
    [self showViewControllerWithIdentifier:@"MyQRCode"];
}

- (void)showViewControllerWithIdentifier:(NSString *) Identifier {
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"My" bundle:[NSBundle mainBundle]];
    UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:Identifier];
    viewController.hidesBottomBarWhenPushed = YES;
    
    [self showViewController:viewController sender:self];
    
}

//小数点
-(NSString *)notRounding:(float)price afterPoint:(int)position{
    
    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    
    NSDecimalNumber *ouncesDecimal;
    
    NSDecimalNumber *roundedOunces;
    
    ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];
    
    roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
   
    return [NSString stringWithFormat:@"%@",roundedOunces];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
